package com.xhtranslate.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author liuxuhuang
 * @version 1.0
 * @package com.xhtranslate.common.util
 * @date 2022/11/21 23:40:04
 * @description 判断是否超额工具类
 */
@Component
public class JudgeCreditLimitUtil {
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 文本翻译,从redis获取使用额度，判断使用额度是否小于免费额度
     * true可用，false不可以
     *
     * @param redisName
     * @param creditLimit
     * @param textLength
     * @return
     */
    public boolean getBeyondTextCredit(String redisName, int creditLimit, int textLength) {
        boolean flag = false;
        if (redisUtil.exists(redisName)) {
            int o = (int) redisUtil.get(redisName);
            if ((o + textLength) < creditLimit) {
                flag = true;
            }
        } else {
            boolean set = redisUtil.set(redisName, 0);
            flag = set;
        }
        return flag;
    }

    /**
     * ocr识别,从redis获取使用额度，判断使用额度是否小于免费额度
     * true可用，false不可以
     *
     * @param redisName   复述,名字
     * @param creditLimit 信用额度
     * @return boolean
     */
    public boolean getBeyondOcrCredit(String redisName, int creditLimit) {
        boolean flag = false;
        if (redisUtil.exists(redisName)) {
            int o = (int) redisUtil.get(redisName);
            if (o < creditLimit) {
                flag = true;
            }
        } else {
            boolean set = redisUtil.set(redisName, 0);
            flag = set;
        }
        return flag;
    }
}
